Skip to content

sqlite: add database.createCollation() - #67772

Open
vedchaudhari wants to merge 1 commit into
nodejs:mainfrom
vedchaudhari:sqlite-collation
Open

vedchaudhari wants to merge 1 commit into
nodejs:mainfrom
vedchaudhari:sqlite-collation

Conversation

@vedchaudhari

Copy link
Copy Markdown
Contributor

Fixes #63316

node:sqlite has no way to register a custom collation, so you can't sort or compare text with your own rules in SQL, and a database whose schema uses a custom collation (for example one made by another app) can't be read:

db.exec('SELECT * FROM artists ORDER BY name COLLATE unicase');
// Error: no such collation sequence: unicase

This adds database.createCollation(name, compare), a wrapper around sqlite3_create_collation_v2():

db.createCollation('unicase', (a, b) => a.localeCompare(b));

The result of compare is converted the same way as the compare function for Array.prototype.sort(), so NaN counts as equal.

SQLite has no way for a collation to report an error. If compare throws, the statement is stopped with sqlite3_interrupt(), and the original exception is thrown from all(), get(), run(), iterate(), exec() and the other methods that run statements.

If the statement was writing, its changes are rolled back, the same as SQLite does for an interrupted write: a commit hook turns the commit into a rollback, and an open transaction is rolled back. Without that, an INSERT into an index that uses the collation could be saved with the entry in the wrong place (PRAGMA integrity_check reported a missing index row).

A few things I'd like opinions on:

  • Interrupting also stops any other statement running on the same connection at that moment. That's how sqlite3_interrupt() works, and it's documented.
  • In exec() inside a transaction, the transaction is rolled back even if the statement that threw was only reading, because there's no way to tell which statement in the string failed.
  • Happy to change the name or the API shape if there's a preference.

cc @trivikr @araujogui

@shbernal I saw your interest in this on the issue, would be great to get your review too.

Assisted-by: an AI coding agent. Reviewed and tested locally by me.

Expose sqlite3_create_collation_v2() so that SQL can sort and compare
text with a JavaScript function, and so that databases whose schema
uses a custom collation can be read.

SQLite has no way for a collation to report an error. If the compare
function throws, the statement is interrupted and the exception is
thrown once the statement returns. If the statement was writing, its
changes are rolled back, the same as SQLite does for any interrupted
write.

Fixes: nodejs#63316
Signed-off-by: vedchaudhari <vedc2853@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/sqlite

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. sqlite Issues and PRs related to the SQLite subsystem. labels Nov 25, 2026
@codecov

codecov Bot commented Nov 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.00000% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.51%. Comparing base (36ca6a7) to head (d60e7d1).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
src/node_sqlite.cc 89.00% 4 Missing and 7 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67772      +/-   ##
==========================================
- Coverage   90.54%   90.51%   -0.04%     
==========================================
  Files         803      805       +2     
  Lines      285302   285859     +557     
  Branches    55074    55186     +112     
==========================================
+ Hits       258324   258740     +416     
- Misses      17104    17204     +100     
- Partials     9874     9915      +41     
Files with missing lines Coverage Δ
src/node_sqlite.h 88.03% <ø> (+0.31%) ⬆️
src/node_sqlite.cc 83.03% <89.00%> (-0.03%) ⬇️

... and 30 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. sqlite Issues and PRs related to the SQLite subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sqlite: support custom collations via sqlite3_create_collation_v2

2 participants